#if UNITY_EDITOR
using System;
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using UnityEditor;

public class PluginSetting : ScriptableObject
{
    public PluginLanguage language = PluginLanguage.Chinese;
    private readonly Dictionary<string, string> m_language = new();

    private static PluginSetting _instance;
    public static PluginSetting Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = Resources.Load<PluginSetting>("PluginSetting");
                if (_instance == null)
                {
                    var files = Directory.GetFiles(Application.dataPath, "PluginSetting.asset", SearchOption.AllDirectories);
                    _instance = CreateInstance<PluginSetting>();
                    var path = files[0].Replace('\\', '/').Replace(Application.dataPath, "Assets");
                    AssetDatabase.CreateAsset(_instance, path);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                }
                InitPlugin();
            }
            return _instance;
        }
    }

    public static Dictionary<string, string> Language => Instance.m_language;

    public static void InitPlugin()
    {
        var languageAsset = Resources.Load<TextAsset>(Instance.language == PluginLanguage.Chinese ? "ChineseLanguage" : "EnglishLanguage");
        var text = System.Text.Encoding.UTF8.GetString(languageAsset.bytes);
        var texts = text.Split(new string[] { "\r\n" }, 0);
        Instance.m_language.Clear();
        foreach (var item in texts)
        {
            var items = item.Split('|');
            if (items.Length != 2)
                continue;
            Instance.m_language[items[0]] = items[1];
        }
    }

#if UNITY_EDITOR || DEBUG
    private GUIStyle GetNodeStyle(ref GUIStyle style, ref GUIStyle style1, string styleName, Action<GUIStyle> action = null)
    {
        if (style1 == null)
            goto JUMP;
        if (style1.normal.background != null)
            return style1;
        if (style == null)
            goto JUMP;
        if (style.normal.background == null)
            goto JUMP;
        goto RET;
    JUMP: style = new GUIStyle(GUI.skin.GetStyle(styleName));
        style.normal.textColor = Color.white;
        style.alignment = TextAnchor.UpperCenter;
        action?.Invoke(style);
    RET: return style1 = style;
    }

    [Header("横向间隔条皮肤")]
    public string horSpaceStyleNames = "ButtonMid";
    [SerializeField]
    private GUIStyle horSpaceStyle = null; private GUIStyle horSpaceStyle1 = null;
    public GUIStyle HorSpaceStyle
    {
        get
        {
            return GetNodeStyle(ref horSpaceStyle, ref horSpaceStyle1, horSpaceStyleNames);
        }
    }
#endif
}

/// <summary>
/// 插件语言
/// </summary>
public enum PluginLanguage
{
    /// <summary>
    /// 英文
    /// </summary>
    English,
    /// <summary>
    /// 中文
    /// </summary>
    Chinese
}
#endif